//
//  MoviePlayerViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "MoviePlayerViewController.h"
#import "AppDelegate.h"
@interface MoviePlayerViewController ()
@end

@implementation MoviePlayerViewController

- (void)viewDidAppear:(BOOL)animated
{
    self.moviePlayer.fullscreen = YES;
}

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor whiteColor];
    [super viewDidLoad];
    //创建播放器
    [self initMoviePlay];
    //播放视屏
    [self.moviePlayer play];
    //添加监听
    [self addNotification];
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
    //不让横屏的时候 appDelegate.allowRotation = NO;即可
}
#pragma mark - 只允许播放界面横屏
// 进入全屏
-(void)begainFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    BOOL landscape = (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight);
    NSLog(@"-------进入全屏-------Currently landscape: %@, width: %.2f, height: %.2f",
          (landscape ? @"Yes" : @"No"),
          [[UIScreen mainScreen] bounds].size.width,
          [[UIScreen mainScreen] bounds].size.height);
}
// 退出全屏
-(void)endFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
        //强制归正：
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val =UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    BOOL landscape = (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight);
    NSLog(@"-------退出全屏-------Currently landscape: %@, width: %.2f, height: %.2f",
          (landscape ? @"Yes" : @"No"),
          [[UIScreen mainScreen] bounds].size.width,
          [[UIScreen mainScreen] bounds].size.height);
    NSLog(@"退出全屏");
}
- (void)initMoviePlay
{
     NSURL *url = [NSURL URLWithString:_mp4_url];
    //创建播放器。
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    //播放器类型 嵌入式：解决上下一曲黑屏问题。
    self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    //自适应宽高
    self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_moviePlayer.view];
    self.moviePlayer.view.frame = self.view.bounds;
    self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
}

- (void)addNotification
{
    //1.监听播放状态。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateChanged) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    //2.监听播放完成.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finished) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    //3.监听全屏。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finished) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
}

- (void)stateChanged
{
    
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"播放");
            [self begainFullScreen];
//            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止");
            break;
        default:
            break;
    }
}

- (void)finished
{
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
    [self endFullScreen];
    //删除通知监听。
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //谁申请 谁释放(问题：第一次dismiss 是从控制器 -> 本控制器 第二次dismiss 是本控制器 -> 前控制器)
    [self dismissViewControllerAnimated:NO completion:nil];
    [self dismissViewControllerAnimated:YES completion:nil];

    
    NSLog(@"完成");
}
@end
